package com.xuxueli.commontdemo.commont.testSocket;
import lombok.extern.slf4j.Slf4j;
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

/**
 * 测试公司socket客户端
 */
@Slf4j
public class TestClientSocketCompany {

    public static void main(String[] args) {
        try {
            Socket socket = new Socket(InetAddress.getLocalHost(), 8682);
            //Socket socket = new Socket("localhost", 1123);
            OutputStream os = socket.getOutputStream();
            byte[] sendValue = "232301FE393031323835303730303030303030303001001E160C0F0B061B000438393836303242363033313730303230313830340100A5".getBytes(StandardCharsets.UTF_8);
            os.write(sendValue);
            os.flush();
            socket.shutdownOutput();
            InputStream s = socket.getInputStream();
            //接收输入的数据
            //失败接受
            byte[] buf = new byte[1024];
//            int len;
//            ByteArrayOutputStream out = new ByteArrayOutputStream();
//            while ((len = s.read(buf)) != -1) {
////                outBuffer.append(new String(buf, 0, len, "UTF-8"));
//                out.write(buf,0,len);
//                out.flush();
//            }
//            socket.shutdownInput();
//            byte[] toByteArray = out.toByteArray();
//            System.out.println("接受服务器字节数据："+toByteArray);
//            String bodyStr = new String(toByteArray, StandardCharsets.UTF_8);
//            System.out.println("接受服务器字符串数据："+bodyStr);
            //成功接受
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            int value = s.read(buf);
            out.write(buf,0,value);
            socket.shutdownInput();
            byte[] toByteArray = out.toByteArray();
            System.out.println("接受服务器字节数据："+toByteArray);
            System.out.println("服务端传来的数据："+new String(toByteArray,StandardCharsets.UTF_8));
        } catch (Exception e) {
            log.error("外源调用异常", e);
            System.out.println(e.toString());
        }
    }


}
